package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.common.constant.CodeEnum;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.AIReadConverter;
import com.cskaoyan.wordmemorize.dao.entity.UserVocAISituationDO;
import com.cskaoyan.wordmemorize.dao.entity.UserVocAISituationOptionDO;
import com.cskaoyan.wordmemorize.dao.mapper.UserVocAISituationMapper;
import com.cskaoyan.wordmemorize.dao.mapper.UserVocAISituationOptionMapper;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.UserVocAISituationDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserVocAISituationCommand;
import com.cskaoyan.wordmemorize.request.UserVocAISituationOptionCommand;
import com.cskaoyan.wordmemorize.service.AIReadService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AIReadServiceImpl implements AIReadService {
    @Resource
    UserVocAISituationOptionMapper vocAISituationOptionMapper;
    @Resource
    UserVocAISituationMapper vocAISituationMapper;
    @Resource
    AIReadConverter aiReadConverter;

    /*
            添加AI阅读场景以及选项信息(后台等级特权管理-AI阅读)
    */
    @Override
    @Transactional
    public void addAISituation(UserVocAISituationCommand command) {
        // 转换command为do并插入
        UserVocAISituationDO aiSituationDO = aiReadConverter.userVocAISituationCommandToDO(command);
        vocAISituationMapper.insert(aiSituationDO);

        // 获取situationId
        Long aiSituationDOId = aiSituationDO.getId();

        // 处理option
        if (command.getOptions() != null && !command.getOptions().isEmpty()) {
            List<UserVocAISituationOptionDO> optionDOS = command.getOptions().stream().map(option -> {
                UserVocAISituationOptionDO aiSituationOptionDO = aiReadConverter.userVocAISituationOptionCommandToDO(option);
                aiSituationOptionDO.setSituationId(aiSituationDOId);
                return aiSituationOptionDO;
            }).collect(Collectors.toList());
            System.out.println(optionDOS);
            vocAISituationOptionMapper.insertBatch(optionDOS);
        }
    }

    /*
            更新AI阅读场景以及选项信息(后台等级特权管理-AI阅读)
    */
    @Override
    public void updateAISituation(UserVocAISituationCommand command) {
        UserVocAISituationDO aiSituationDO = aiReadConverter.userVocAISituationCommandToDO(command);
        Long aiSituationDOId = aiSituationDO.getId();
        vocAISituationMapper.updateById(aiSituationDO);
        List<UserVocAISituationOptionDO> optionDOS = command.getOptions().stream().map(option -> {
            UserVocAISituationOptionDO aiSituationOptionDO = aiReadConverter.userVocAISituationOptionCommandToDO(option);
            return aiSituationOptionDO;
        }).collect(Collectors.toList());
        if (optionDOS!=null &&!optionDOS.isEmpty()) {
            for (UserVocAISituationOptionDO optionDO : optionDOS) {
                if (optionDO.getId()!=null){
                    vocAISituationOptionMapper.updateById(optionDO);
                }else{
                    optionDO.setSituationId(aiSituationDOId);
                    vocAISituationOptionMapper.insert(optionDO);
                }

            }
        }
    }

    /*
        删除AI阅读场景(后台等级特权管理-AI阅读)
     */
    @Override
    @Transactional
    public void deleteAISituation(Long id) {
        vocAISituationOptionMapper.deleteBySituationId(id);

        vocAISituationMapper.deleteById(id);
    }

    /*
             删除AI阅读场景中的某个选项(后台等级特权管理-AI阅读)
     */
    @Override
    public void deleteAISituationOption(Long id) {
        UserVocAISituationOptionDO aiSituationOptionDO = vocAISituationOptionMapper.selectById(id);
        if (aiSituationOptionDO == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        vocAISituationOptionMapper.deleteById(id);


    }

    /*
           批量删除AI阅读场景(后台等级特权管理-AI阅读)
    */
    @Override
    @Transactional
    public void batchDeleteAISituationOption(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return;
        }
        vocAISituationOptionMapper.deleteBySituationIds(ids);

        vocAISituationMapper.deleteByIds(ids);
    }

    /*
          分页查询AI阅读场景信息(后台等级特权管理-AI阅读)
    */
    @Override
    public PageDTO<UserVocAISituationDTO> getAISituationList(PageRequest pageRequest) {
        // Page<UserVocAISituationDO> page = new Page<>(pageRequest.getPageNum(), pageRequest.getCount());
        // LambdaQueryWrapper<UserVocAISituationDO> queryWrapper=new LambdaQueryWrapper<>();
        // if (pageRequest.getKeyword()!=null){
        //     queryWrapper.like(UserVocAISituationDO::getName,pageRequest.getKeyword());
        //     vocAISituationMapper.selectPage(page,queryWrapper);
        //
        //     return aiReadConverter.toUserVocAISituationPage(page.getRecords(),page.getTotal());
        // }

        List<UserVocAISituationDO> userVocAISituationDOPage = vocAISituationMapper.queryAISituationList(pageRequest);
        // List<UserVocAISituationDO> records = userVocAISituationDOPage.getRecords();
        QueryWrapper<UserVocAISituationDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(pageRequest.getKeyword()!=null,"name",pageRequest.getKeyword());
        List<UserVocAISituationDO> userVocAISituationDOS = vocAISituationMapper.selectList(queryWrapper);
        return aiReadConverter.toUserVocAISituationPage(userVocAISituationDOPage, Long.valueOf(userVocAISituationDOS.size()));
    }
}
